/**
 * @file skoll.actions.content.image.js
 * @author Alejandro Dario Simi
 * @date $Date: 2014-06-03 03:40:32 +0000 (Tue, 03 Jun 2014) $
 *
 * $Id: skoll.actions.content.image.js 112 2014-06-03 03:40:32Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/skins/Skoll/js/skoll.actions.content.image.js $
 */

Skoll.actions.content.image = {
	//
	// Protected variables.
	_shortcutsButtons: {
		F: "img.imgfirst",
		C: "img.imgprev5",
		Z: "img.imgprev",
		A: "img.imgcurrent",
		X: "img.imgnext",
		V: "img.imgnext5",
		L: "img.imglast",
		T: "#TagButton"//,		R: "#ReadLight"
	},
	_shortcutsRating: {
		"1": 1,
		"2": 2,
		"3": 3,
		"4": 4,
		"5": 5,
		"6": 6,
		"7": 7,
		"8": 8,
		"9": 9,
		"0": 10
	},
	//
	// Public methods.
	addShortCuts: function() {
		Skoll.tools.ShortCuts.enable();
		for (var key in Skoll.actions.content.image._shortcutsButtons) {
			Skoll.tools.ShortCuts.addClick(key, Skoll.actions.content.image._shortcutsButtons[key]);
		}
		for (var key in Skoll.actions.content.image._shortcutsRating) {
			Skoll.tools.ShortCuts.add(key, function(key, event) {
				Skoll.actions.content.image.changeRate(event, Skoll.actions.content.image._shortcutsRating[key]);
			});
		}
	},
	changeRate: function(event, value) {
		Skoll.tools.preventDefault(event);
//		Skoll.tools.Loading.start();

		$("#ImagePersonalRate").text(wcomix.lang.translate("message_loading___"));

		$.ajax({
			dataType: "json",
			url: "service.php?service=readrate:rate:image&image=" + IMAGE_ID + "&group=" + GROUP_ID + "",
			type: "POST",
			data: {
				params: JSON.stringify({
					rate: value
				})
			},
			success: function(response) {
				if (typeof response.status !== "undefined" && response.status) {
					Skoll.tools.Baloon.message(wcomix.lang.translate("message_you_rated_as_of", {
						name: IMAGE_NAME,
						count: value,
						total: 10
					}));

					Skoll.actions.content.image.loadReadRate();
//
//					for (var shortcut in Skoll.actions.content.image._shortcutsRating) {
//						shortcut.remove(key);
//					}
				} else {
					Skoll.tools.log(response);
				}

//				Skoll.tools.Loading.stop();
			},
			error: function(response) {
				Skoll.tools.log(response);
//				Skoll.tools.Loading.stop();
			}
		});
	},
	loadImage: function(linkObj) {
		return true;
	},
	loadReadRate: function() {
		$.ajax({
			url: "service.php?service=readrate:get:image",
			data: {
				group: GROUP_ID,
				image: IMAGE_ID
			},
			dataType: "json",
			success: function(response) {
				console.log(response);
				if (typeof response.status !== "undefined" && response.status) {
					var text = wcomix.lang.translate("text_counts_of_total", {
						total: 10,
						count: response.data.global.rate
					});
					text += " (";
					text += wcomix.lang.translate("text_count_votes", {
						count: response.data.global.votes
					});
					text += ")";

					$("#ImageGlobalRate").text(text);

					$("#ImageGlobalRate").text(text);

					text = "";
					if (response.data.personal.pending) {
						text = wcomix.lang.translate("label_user_rate_pending_image");
					} else {
						text = wcomix.lang.translate("text_counts_of_total", {
							total: 10,
							count: response.data.personal.rate
						});
					}
					$("#ImagePersonalRate").text(text);

					Skoll.actions.content.image._isRead = response.data.personal.read;
					if (Skoll.actions.content.image._isRead) {
						$("#ImagePersonalRead").text(wcomix.lang.translate("label_read"));
					} else {
						$("#ImagePersonalRead").text(wcomix.lang.translate("label_unread"));
					}
				} else {
					Skoll.tools.log(response);
				}
			},
			error: function(response) {
				Skoll.tools.log(response);
			}
		});
	},
	shortCutsHelp: function() {
		Skoll.tools.preventDefault();
		Skoll.tools.ModalWindow.openSimpleModal($("#ShortcutsHelp").html());
	},
	//
	// Protected methods.
	_binds: function() {
		$("#ImagePersonalRead").on("click", function(event) {
			if (Skoll.actions.content.image._isRead !== null) {
				Skoll.tools.preventDefault(event);
//				Skoll.tools.Loading.start();

				var currentStatus = Skoll.actions.content.image._isRead;
				Skoll.actions.content.image._isRead = null;

				$("#ImagePersonalRead").text(wcomix.lang.translate("message_loading___"));


				$.ajax({
					dataType: "json",
					url: "service.php?service=readrate:read:image&image=" + IMAGE_ID + "&group=" + GROUP_ID + "",
					type: "POST",
					data: {
						params: JSON.stringify({
							read: !currentStatus
						})
					},
					success: function(response) {
						if (typeof response.status !== "undefined" && response.status) {
							Skoll.tools.Baloon.message(wcomix.lang.translate("message_you_marked_as", {
								name: IMAGE_NAME,
								status: wcomix.lang.translate(currentStatus ? "label_unread" : "label_read")
							}));

							Skoll.actions.content.image.loadReadRate();
						} else {
							Skoll.tools.log(response);
						}

//						Skoll.tools.Loading.stop();
					},
					error: function(response) {
						Skoll.tools.log(response);
//						Skoll.tools.Loading.stop();
					}
				});
			}
		});
	}
};

$(document).ready(function() {
//	$("input[type=text]").focusin(Skoll.actions.content.image._removeShortCuts).focusout(Skoll.actions.content.image._addShortCuts);
//	$("textarea").focusin(Skoll.actions.content.image._removeShortCuts).focusout(Skoll.actions.content.image._addShortCuts);
	Skoll.actions.content.image._binds();
});
